/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * BolsaClient.java
 *
 * Created on 21/11/2008, 02:45:29 PM
 */

package bolsa;

import Comunicacion.ClientCom;
import clients.ComListener;

/**
 *
 * @author michael
 */
public class BolsaClient extends javax.swing.JFrame implements ComListener{
    private ClientCom com;
    private Portafolio portafolio;
    /** Creates new form BolsaClient */
    public BolsaClient() {
        com = new ClientCom(this);
        portafolio = new Portafolio();
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        loginFrame = new javax.swing.JFrame();
        nomAccionLabel = new javax.swing.JLabel();
        coredorField = new javax.swing.JTextField();
        accionField = new javax.swing.JTextField();
        conectarButton = new javax.swing.JButton();
        nomCorrLabel = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        messageArea = new javax.swing.JTextArea();
        inputField = new javax.swing.JTextField();
        sendButton = new javax.swing.JButton();
        crearAccionesBtn = new javax.swing.JButton();
        venderAccionesBtn = new javax.swing.JButton();
        accionesLabel = new javax.swing.JLabel();
        crearAccionesField = new javax.swing.JTextField();
        venderAccionesField = new javax.swing.JTextField();

        nomAccionLabel.setText("Nombre Accion");

        coredorField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                coredorFieldActionPerformed(evt);
            }
        });

        conectarButton.setText("Conectar");
        conectarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                conectarButtonActionPerformed(evt);
            }
        });

        nomCorrLabel.setText("Nombre Corredor");

        javax.swing.GroupLayout loginFrameLayout = new javax.swing.GroupLayout(loginFrame.getContentPane());
        loginFrame.getContentPane().setLayout(loginFrameLayout);
        loginFrameLayout.setHorizontalGroup(
            loginFrameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(loginFrameLayout.createSequentialGroup()
                .addGroup(loginFrameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(conectarButton)
                    .addGroup(loginFrameLayout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(loginFrameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(nomAccionLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 105, Short.MAX_VALUE)
                            .addComponent(accionField, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(loginFrameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(nomCorrLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(coredorField, javax.swing.GroupLayout.Alignment.LEADING))))
                .addContainerGap())
        );
        loginFrameLayout.setVerticalGroup(
            loginFrameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(loginFrameLayout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addGroup(loginFrameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nomCorrLabel)
                    .addComponent(nomAccionLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(loginFrameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(coredorField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(accionField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(30, 30, 30)
                .addComponent(conectarButton)
                .addContainerGap())
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        messageArea.setColumns(20);
        messageArea.setRows(5);
        jScrollPane1.setViewportView(messageArea);

        sendButton.setText("Send");
        sendButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sendButtonActionPerformed(evt);
            }
        });

        crearAccionesBtn.setText("Crear Acciones");
        crearAccionesBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                crearAccionesBtnActionPerformed(evt);
            }
        });

        venderAccionesBtn.setText("Vender");
        venderAccionesBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                venderAccionesBtnActionPerformed(evt);
            }
        });

        crearAccionesField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                crearAccionesFieldActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(sendButton))
            .addComponent(inputField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addComponent(crearAccionesBtn)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(crearAccionesField, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 47, Short.MAX_VALUE)
                .addComponent(accionesLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(40, 40, 40)
                .addComponent(venderAccionesBtn)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(venderAccionesField, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 213, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(layout.createSequentialGroup()
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(crearAccionesBtn)
                                .addComponent(crearAccionesField, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 9, Short.MAX_VALUE))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(7, 7, 7)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(venderAccionesField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(venderAccionesBtn))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(accionesLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addComponent(inputField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(sendButton))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void conectarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_conectarButtonActionPerformed
        String msg = "connect " + accionField.getText();
        System.out.println(msg);
        this.com.out.println(msg);
        msg = "clientname " + coredorField.getText();
        System.out.println(msg);
        this.com.out.println(msg);
        portafolio.setName(coredorField.getText());
        this.loginFrame.setVisible(false);
        this.setVisible(true);
    }//GEN-LAST:event_conectarButtonActionPerformed

    private void sendButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sendButtonActionPerformed
        String msg = inputField.getText();
        this.com.out.println(msg);
        inputField.setText("");
    }//GEN-LAST:event_sendButtonActionPerformed

    private void coredorFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_coredorFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_coredorFieldActionPerformed

    private void venderAccionesBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_venderAccionesBtnActionPerformed
        com.out.println("vende "+venderAccionesField.getText());
}//GEN-LAST:event_venderAccionesBtnActionPerformed

    private void crearAccionesFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_crearAccionesFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_crearAccionesFieldActionPerformed

    private void crearAccionesBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_crearAccionesBtnActionPerformed
        int cantidad = Integer.parseInt(crearAccionesField.getText());
        portafolio.agregarAcciones(cantidad);
        accionesLabel.setText("" + portafolio.getCantidad());
        
}//GEN-LAST:event_crearAccionesBtnActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                BolsaClient bc = new BolsaClient();
                bc.showLogin();
            }
        });
    }

    public void showLogin(){
        this.loginFrame.setSize(300, 300);
        this.loginFrame.setVisible(true);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField accionField;
    private javax.swing.JLabel accionesLabel;
    private javax.swing.JButton conectarButton;
    private javax.swing.JTextField coredorField;
    private javax.swing.JButton crearAccionesBtn;
    private javax.swing.JTextField crearAccionesField;
    private javax.swing.JTextField inputField;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JFrame loginFrame;
    private javax.swing.JTextArea messageArea;
    private javax.swing.JLabel nomAccionLabel;
    private javax.swing.JLabel nomCorrLabel;
    private javax.swing.JButton sendButton;
    private javax.swing.JButton venderAccionesBtn;
    private javax.swing.JTextField venderAccionesField;
    // End of variables declaration//GEN-END:variables

    public void receiveMsg(String msg) {
        if(msg.contains("compro") && !msg.startsWith(portafolio.getName())){
            int cant = Integer.parseInt(msg.substring(msg.indexOf("compro")+7));
            if(portafolio.quitarAcciones(cant)){
                accionesLabel.setText("" + portafolio.getCantidad());
            }
        }
        if(msg.contains("compro")&& msg.startsWith(portafolio.getName())){
            int cant = Integer.parseInt(msg.substring(msg.indexOf("compro")+7));
            portafolio.agregarAcciones(cant);
             accionesLabel.setText("" + portafolio.getCantidad());
        }
        this.messageArea.append(msg+"\n");
    }

}
